import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Huandeng extends JFrame {

	private int imgCount;
	private JPanel panel ;
	private static  java.util.Timer timer; 
	private  final int IMGCOUNTS =40;
	/**
	 * Launch the application
	 * @param args
	 */

	/**
	 * Create the application
	 */
	public Huandeng() {
		imgCount=1;
		createContents();
	}

	/**
	 * Initialize the contents of the frame
	 */
	private void createContents() {
		setTitle("图片展示");
		getContentPane().setLayout(null);
		setResizable(false);
		setBounds(100, 100, 985, 750);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE); 

	    panel = new GetPanel(960,680,System.getProperty("user.dir")+"/picture/幻灯片"+imgCount+".jpg");
		panel.setBounds(10, 10, 960, 720);
		getContentPane().add(panel);
		//��һ�Ű�ť
		final JButton button = new JButton();
		button.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		button.setText("下一张");
		button.setBounds(770, 633, 90, 42);
		button.setOpaque(false);
		getContentPane().add(button);
		//��һ�Ű�ť
		final JButton button_1 = new JButton();
		button_1.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		button_1.setText("上一张");
		button_1.setBounds(668, 633, 90, 42);
		button_1.setOpaque(false);
		getContentPane().add(button_1);
		//���¿�ʼ��ť
		final JButton button_0 = new JButton();
		button_0.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		button_0.setText("重新开始");
		button_0.setBounds(870, 633, 90, 42);
		getContentPane().add(button_0);
		//����
		button_0.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent   arg0){
				if(MessageBox.Show("你确定要重新开始吗？", 12)==0){
					imgCount=2;
					changeBgImg(true);
				}
			}
		});
		button_1.addActionListener(new ActionListener(){
			public   void   actionPerformed(ActionEvent   arg0) {
				changeBgImg(true);
			}
		});
		button.addActionListener(new ActionListener(){
			public   void   actionPerformed(ActionEvent   arg0) {
				changeBgImg(false);
			}
		});
		panel.addMouseListener(mouse);
		
	}
	private MouseListener mouse = new MouseListener() {

		@Override
		public void mouseClicked(MouseEvent arg0) {
			int x = 2*arg0.getX();
			changeBgImg(x<=panel.getWidth());
		}
		@Override
		public void mouseEntered(MouseEvent e) {
			
		}

		@Override
		public void mouseExited(MouseEvent e) {
			
		}

		@Override
		public void mousePressed(MouseEvent e) {
			
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			
		}
		
	};
	private  void  changeBgImg(Boolean previous){
		if(previous){
			if(imgCount==1){
				MessageBox.Show("已经到第一张");
			}else{
				imgCount--;
			}
		}else {
			if(imgCount==40){
				MessageBox.Show("已经到最后一张");
			}else {
				imgCount++;
			}
		}
		getContentPane().remove(panel);
		panel = new GetPanel(960,680,System.getProperty("user.dir")+"/picture/幻灯片"+imgCount+".jpg");
		panel.setBounds(10, 10, 960, 720);
		panel.addMouseListener(mouse);
		getContentPane().add(panel);
		repaint();
	}


}
